import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池
 */
public class ThreadPool {
    public static void main(String[] args) {
        // 创建一个线程池，无上限的池子
        ExecutorService executorService = Executors.newCachedThreadPool();
        // 创建一个个数为3个的线程池
        ExecutorService executorService2 = Executors.newFixedThreadPool(3);

        // 提交任务
        executorService.submit(new MyThread("A"));
        executorService.submit(new MyThread("B"));
        executorService.submit(new MyThread("C"));

        executorService2.submit(new MyThread("A"));
        executorService2.submit(new MyThread("B"));
        executorService2.submit(new MyThread("C"));
        executorService2.submit(new MyThread("D"));
        executorService2.submit(new MyThread("E"));
        executorService2.submit(new MyThread("F"));
        executorService2.submit(new MyThread("G"));



        //销毁线程池
        executorService.shutdown();
        executorService2.shutdown();


    }

    private static class MyThread extends Thread{
        @Override
        public void run() {
            String name = getName();
            for(int i = 0; i < 10; i++){
                System.out.println(name + i);
            }
        }

        public MyThread(String name) {
            super(name);
        }
    }
}
